package com.rao.mideng.core;

import com.rao.mideng.annotation.RaoLogAnno;
//import lombok.ToString;
import org.aopalliance.aop.Advice;
import org.springframework.aop.Pointcut;
import org.springframework.aop.support.AbstractPointcutAdvisor;
import org.springframework.aop.support.ComposablePointcut;
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;


//@ToString
public class LogAnnoAdvisor extends AbstractPointcutAdvisor {
    private LogInterceptor logInterceptor;
    private Advice advice;
    private Pointcut pointcut;

    public LogAnnoAdvisor(LogInterceptor logInterceptor) {
        this.logInterceptor = logInterceptor;
        this.advice = this.buildAdvice();
        this.pointcut = this.buildPointcut();

    }

    public Pointcut getPointcut() {
        return this.pointcut;
    }

    public Advice getAdvice() {
        return this.advice;
    }

    protected Advice buildAdvice() {
        return this.logInterceptor;
    }

    protected Pointcut buildPointcut() {
        Pointcut cpc = new AnnotationMatchingPointcut(RaoLogAnno.class, true);
        Pointcut mpc = AnnotationMatchingPointcut.forMethodAnnotation(RaoLogAnno.class);
        ComposablePointcut result = (new ComposablePointcut(cpc)).union(mpc);
        return result;
    }
}
